// pyctptdif.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "utility.h"
#include "mylog.h"
#include "datatype.h"
#include "ChineseCodeLib.H"

#include <ThostFtdcTraderApi.h>
#include "datastructio.h"
#include "BaseTradeSpi.h"
#include "tradedata.h"
#include "Trade.h"


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		#	ifdef _DEBUG
				CheckMemoryLeak;
		#	endif
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}



using namespace CTPTDIF;

inline char get_price_condition(char cPriceCondition)
{
	return ((cPriceCondition < 9) ? cPriceCondition + '1' : ( (cPriceCondition < 15) ? (cPriceCondition - 9 + 'A') : ('H')) );
}


CTrade      gTrade4Run;
HANDLE ghFortuneThread;
DWORD     gdwReceivers;
static CtpifProfile gprofile;

DWORD WINAPI TdifThread(LPVOID param)
{
	gTrade4Run.setup((CtpifProfile*)param);
	gTrade4Run.init();

	return (0);
}

/*
HANDLE StartTdifThread(CtpifProfile * profile, DWORD * dwThreadID)
{
	ghFortuneThread = CreateThread(NULL, 0, TdifThread, profile, 0, &gdwReceivers);
	return ghFortuneThread;
}

void StopTdifThread(DWORD dwExitCode)
{
	gTrade4Run.logout();
	gTrade4Run.release();

	TerminateThread(ghFortuneThread, dwExitCode);
	ghFortuneThread = 0;
}

CTrade * GetTdifTrader()
{
	return &gTrade4Run;
}


int query_instrument(const char *instrument = NULL)
{
    return gTrade4Run.query_instrument(instrument);
}

int query_account()
{
    return gTrade4Run.query_account();
}

int query_investor_position(const char *instrument = NULL)
{
    return gTrade4Run.query_investor_position(instrument);
}

int query_order(const char * instrument = NULL,
				const char * exchange   = NULL,
				const char * ordersysid = NULL,
				const char * begintime  = NULL,
				const char * endtime    = NULL)
{
    return gTrade4Run.query_order(instrument, exchange, ordersysid, begintime, endtime);
}

int query_trade(const char * instrument = NULL,
				const char * exchange   = NULL,
				const char * tradeid    = NULL,
				const char * begintime  = NULL,
				const char * endtime    = NULL)
{
    return gTrade4Run.query_trade(instrument, exchange, tradeid, begintime, endtime);
}


int order_insert_limit_price(const char *instrument, char cDirection, double fPrice, int nVolume,
							    char cVolumeCondition,  const char * strGTDdate, char cTimeCondition,
								int nMinVolume, char cTrigerCondition, double fStopPrice)
{
    return gTrade4Run.order_insert_limit_price(instrument, cDirection, fPrice, nVolume,
							    cVolumeCondition,  strGTDdate, cTimeCondition,
								nMinVolume, cTrigerCondition, fStopPrice);
}


int order_insert_limitd(const char *instrument, char cDirection, double fPrice, int nVolume)
{
    return gTrade4Run.order_insert_limitd(instrument, cDirection, fPrice, nVolume);
}

int order_insert_condtn(const char *instrument, char cDirection, double fPrice, int nVolume, double fConditionPrice, char cTrigerCondition, char cPriceCondition)
{
    return gTrade4Run.order_insert_condtn(instrument, cDirection, fPrice, nVolume, fConditionPrice, cTrigerCondition, cPriceCondition);
}

int order_corver_condtn(const char *instrument, char cDirection, double fPrice, int nVolume, double fConditionPrice, char cTrigerCondition, char cPriceCondition)
{
    return gTrade4Run.order_corver_condtn(instrument, cDirection, fPrice, nVolume, cPriceCondition, cTrigerCondition, cPriceCondition);
}


int order_corver(const char *instrument, char cDirection, double fPrice, int nVolume)
{
    return gTrade4Run.order_corver(instrument, cDirection, fPrice, nVolume);
}

int order_recall(ORDER * order)
{
    return gTrade4Run.order_recall(order);
}


char * GetOrderStatus(char status, char * buf, int len)
{
    return gTrade4Run.GetOrderStatus(status, buf, len);
}
*/



static PyObject *
start (PyObject *self, PyObject *args)//CtpifProfile * profile, DWORD * dwThreadID
{
	return Py_BuildValue("i", 0);
}


static PyObject *
stop(PyObject *self, PyObject *args)//DWORD dwExitCode)
{
	return Py_BuildValue("i", 0);
}



static PyObject *
query_instr(PyObject *self, PyObject *args)//const char *instrument = NULL)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
query_account(PyObject *self, PyObject *args)//)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
query_invpos(PyObject *self, PyObject *args)//const char *instrument = NULL)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
query_order(PyObject *self, PyObject *args)
				//const char * instrument = NULL,
				//const char * exchange   = NULL,
				//const char * ordersysid = NULL,
				//const char * begintime  = NULL,
				//const char * endtime    = NULL)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
query_trade(PyObject *self, PyObject *args)
				//const char * instrument = NULL,
				//const char * exchange   = NULL,
				//const char * tradeid    = NULL,
				///const char * begintime  = NULL,
				//onst char * endtime    = NULL)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
order_insert_limitd(PyObject *self, PyObject *args)
					//const char *instrument,
					//char cDirection,
					//double fPrice, int nVolume)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
order_insert_condtn(PyObject *self, PyObject *args)
					//const char *instrument, char cDirection, double fPrice, int nVolume, double fConditionPrice, char cTrigerCondition, char cPriceCondition)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
order_corver_condtn(PyObject *self, PyObject *args)
					//const char *instrument, char cDirection, double fPrice, int nVolume, double fConditionPrice, char cTrigerCondition, char cPriceCondition)
{
	return Py_BuildValue("i", 0);
}


static PyObject *
order_corver(PyObject *self, PyObject *args)
			//const char *instrument, char cDirection, double fPrice, int nVolume)
{
	return Py_BuildValue("i", 0);
}

static PyObject *
order_recall(PyObject *self, PyObject *args)
			//ORDER * order)
{
	return Py_BuildValue("i", 0);
}


static PyObject *
order_status(PyObject *self, PyObject *args)
			//char status, char * buf, int len)
{
	return Py_BuildValue("i", 0);
}




PyObject * reg_struct_mod = NULL;
PyObject * reg_struct(PyObject * self, PyObject * args)
{
	reg_struct_mod = PyTuple_GET_ITEM(args,0);
	Py_INCREF(Py_None);
	return Py_None;
}



static PyObject *
ex_foo(PyObject *self, PyObject *args)
{
	printf("Hello, world\n");
	Py_INCREF(Py_None);
	return Py_None;
}


static PyObject* profile(PyObject* /*self*/, PyObject* args)
{
	char module_name[MAX_PATH];
    const char *filepath;

	//mod = PyTuple_GET_ITEM(args,0);
	//printf("0x%x\n", mod);

	PyArg_ParseTuple(args, "s", &filepath);
	printf("pyctptdif: configure file is %s\n", filepath);

	CChineseCodeLib::UnicodeToGB2312(module_name, MAX_PATH, _T("pyctptdif"));
	if (get_configure(filepath, &gprofile, module_name))
		return Py_BuildValue("i", -1);

	return Py_BuildValue("i", 0);
}

static PyMethodDef example_methods[] = {
	{"profile"            , profile            , METH_VARARGS, "profile                "},	
	{"start"              , start              , METH_VARARGS, "start                  "},
	{"stop"               , stop               , METH_VARARGS, "stop                   "},
	{"query_instr"        , query_instr        , METH_VARARGS, "query_instrument       "},
	{"query_account"      , query_account      , METH_VARARGS, "query_account          "},
	{"query_invpos"       , query_invpos       , METH_VARARGS, "query_investor_position"},
	{"query_order"        , query_order        , METH_VARARGS, "query_order            "},
	{"query_trade"        , query_trade        , METH_VARARGS, "query_trade            "},
	{"order_insert_limitd", order_insert_limitd, METH_VARARGS, "order_insert_limitd    "},
	{"order_insert_condtn", order_insert_condtn, METH_VARARGS, "order_insert_condtn    "},
	{"order_corver_condtn", order_corver_condtn, METH_VARARGS, "order_corver_condtn    "},
	{"order_corver"       , order_corver       , METH_VARARGS, "order_corver           "},
	{"order_recall"       , order_recall       , METH_VARARGS, "order_recall           "},
	{"order_status"       , order_status       , METH_VARARGS, "order_status           "},
	{"reg_struct", reg_struct, METH_VARARGS, "register data struct" },
	{"foo " , ex_foo, METH_VARARGS, "foo() doc string"},
	{NULL, NULL}
};

PyMODINIT_FUNC
initpyctptdif(void)
{
	Py_InitModule("pyctptdif", example_methods);

	PyEval_InitThreads();
}

